home *** CD-ROM | disk | FTP | other *** search
- CUU1@VAXA.NEWCASTLE-POLY.AC.UK (David Hardy) / 12:23 pm Feb 10, 1992 */
- In message <01GG0A5BZP00001MSC@EKU.BITNET>, David Barbee
- (<STUBRBEE%bitnet.EKU@edu.CUNY.CUNYVM>) writes:
-
- > I'm looking for an algorithm or source code (preferably pascal) to
- > calculate what day of the week any given day is on. I know that asking
- > for ANY date is a little much...but i would like for it to be accurate
- > as possible.
-
- As several Info-VAXers have noted, library routine LIB$DAY_OF_WEEK will
- return the required information on a VMS system. If the program is to run on
- several platforms, however, another method is needed. The program below
- contains a routine (DOW) which takes the day, month and year (as integers
- and with the year containing the century) and returns an integer indicating
- the day of the week with Sunday as 0, Monday as 1, etc. The algorithm
- doesn't check that the date is reasonable (you can enter 31 2 1992 for
- February 31st, 1992, for example) and it doesn't work for dates before the
- change to the Gregorian calendar which took place on the 2nd September, 1752
- (and made the next date the 14th September). The curious formula
-
- weekday := (260 * month - 19) div 100 + day + year + year div 4 +
- century div 4 - 2 * century;
-
- was devised by the Reverend Zeller and means that the algorithm doesn't need
- to use a look-up table of month lengths. The algorithm also adjusts the year
- to start on March 1st as this means that February 29th falls at the end of a
- year and doesn't require special-case handling.
-
- I converted the algorithm from BBC BASIC to Pascal and the original program
- is listed in the book "132 Short Programs for the Mathematics Classroom" by
- The Mathematical Association which is published by Stanley Thornes
- (Publishers) Ltd (ISBN 0-85950-556-1). I have no further reference for the
- algorithm itself.
-
- The DOW routine should work if transferred to another program but you should
- satisfy yourself that it does what you want - I take no responsibility for
- errors should there be any.
-
- ============================================================================
-
- program weekday(input,output);
-
- var
- d, m, y : integer;
-
- function dow(day, month, year : integer) : integer;
-
- var
- century, weekday : integer;
-
- begin
- if (month = 1) or (month = 2) then
- begin
- month := month + 12;
- year := year - 1;
- end;
- month := month - 2;
- century := year div 100;
- year := year mod 100;
- weekday := (260 * month - 19) div 100 + day + year + year div 4 +
- century div 4 - 2 * century;
- dow := weekday mod 7;
- end;
-
- begin
- repeat
- write('Enter date in form d m y (include century) or 0 0 0 to end ');
- readln(d,m,y);
- if (d > 0) then
- case dow(d,m,y) of
- 0 : writeln('Sunday');
- 1 : writeln('Monday');
- 2 : writeln('Tuesday');
- 3 : writeln('Wednesday');
- 4 : writeln('Thursday');
- 5 : writeln('Friday');
- 6 : writeln('Saturday');
- end;
- until d = 0;
- end.
-
- ============================================================================
-
- +----------------------------------------------------------------+
- | David Hardy |
- | Computer Unit, Newcastle Polytechnic, Northumberland Building, |
- | Ellison Place, Newcastle upon Tyne, NE1 8ST, U.K. |
- +----------------------------------------------------------------+
- | JANET: DAVID.HARDY@NPY.AC.UK |
- +----------------------------------------------------------------+
-
- /* ---------- */
-
-